<template>
  <el-card shadow="never">
    <el-button
      class="mb20"
      size="small"
      icon="el-icon-plus"
      type="primary"
      @click="handleEdit(false, {})"
    />
    <e-table
      ref="eTable"
      @getList="handleGetList"
      :tableData="tableData"
      :columns="columns"
      :loading="loading"
      :border="false"
      :pageObj="null"
      :rowKey="'categoryId'"
      :treeProps="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="首页展示" slot="homeFlag" align="center">
        <template slot-scope="scope">{{ scope.row.homeFlag === 1 ? '是' : '否' }}</template>
      </el-table-column>
      <el-table-column slot="action" fixed="right" width="50" align="center">
        <template slot-scope="scope">
          <el-dropdown>
            <a class="el-dropdown-link">
              <i class="el-icon-menu"></i>
            </a>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleEdit(true, scope.row)">
                <a>编辑</a>
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(scope.row)">
                <a class="red">删除</a>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </e-table>
    <column-modal @refresh="handleRefresh" ref="columnModal" />
  </el-card>
</template>

<script>
import { getCms, deleteCms } from '@/api/system'
import ColumnModal from './ColumnModal'
import { openNotification } from '@/utils/util'

export default {
  components: {
    ColumnModal
  },
  data() {
    return {
      tableData: [],
      columns: [
        {
          label: '栏目名称',
          prop: 'categoryName'
        },
        {
          slot: 'homeFlag'
        },
        {
          slot: 'action'
        }
      ],
      total: 1,
      loading: false,
      queryParam: {
        pageSize: 10,
        pageNo: 1
      }
    }
  },
  methods: {
    handleEdit(bool, record) {
      this.$nextTick(() => {
        this.$refs.columnModal.edit(bool, record)
      })
    },
    async handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      const res = await getCms(this.queryParam)
      if (res.data && res.data.length > 0) {
        const { state, data } = res
        this.tableData = data
      } else {
        this.tableData = []
      }
      this.loading = false
    },
    // 删除
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.categoryName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deleteCms(row.categoryId, {})
          openNotification('success', '删除成功')
          _this.handleGetList()
        }
      })
    },
    handleRefresh() {
      this.handleGetList()
    }
  },
  mounted() {
    this.handleGetList()
  }
}
</script>
